package com.epalmpay.commom;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.RoleInDTO;
import com.epalmpay.dto.system.RoleOutDTO;
import com.epalmpay.entity.Role;
import com.epalmpay.entity.Userbase;
import com.epalmpay.service.management.IButtonService;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.service.management.IRoleService;
import com.epalmpay.service.management.IUserService;
import com.epalmpay.util.PropertyCopyUtil;

@Controller
@RequestMapping(value="/manager/role")
public class RoleController extends BaseController {
     
	@Resource  
    private IRoleService roleService;  
	@Resource  
    private IUserService userService;  
	@Resource  
    private IMenuService menuService;  
	@Resource  
    private IButtonService buttonService; 
		
	
		
	/**
	 * 列出此组下级角色
	 * 
	 */
	@RequestMapping(value="/listAllRolesByPId")
	@ResponseBody
	public RoleOutDTO  listAllRolesByPId(RoleInDTO roleIn){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		ArrayList<RoleOutDTO> userOutList=new ArrayList<RoleOutDTO>();
		Role role=new Role();
		try {  
			role.setRoleId(roleIn.getRoleId());
			role.setGroupId(roleIn.getGroupId());
			List<Role> roleList=roleService.listAllRolesByPId(role);
			for (int i = 0; i < roleList.size(); i++) {
				RoleOutDTO roles=new RoleOutDTO();
				PropertyCopyUtil.copyPropertiesExclude(roleList.get(i),roles,"role");
				userOutList.add(roles);
			}
			roleOutDTO.setRoleList(userOutList);
			roleOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功"); 
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	
	/**
	 * 修改角色信息
	 * 
	 */
	@RequestMapping(value="/updateRole")
	@ResponseBody
	public RoleOutDTO  updateRole(RoleInDTO roleOut){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		try {  
			Role role=roleService.getRoleById(roleOut.getRoleId());
			role.setRoleName(roleOut.getRoleName()); 
			roleService.edit(role);
			roleOutDTO.setErrorCode(Rescode.SUCCESS, "角色修改成功");
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return roleOutDTO;
	}
	
	
	
	/**
	 * 添加角色信息
	 * 
	 */
	@RequestMapping(value="/saveRole")
	@ResponseBody
	public RoleOutDTO  saveRole(RoleInDTO roleOut){
		RoleOutDTO RoleOutDTO=new RoleOutDTO();
		Role role=new Role();
		try {  
			if(roleOut.getParentId().equals("0")){
				role.setRights("");
			}else{
				Role roles=new Role();
				roles.setRoleId(roleOut.getParentId()); 
				Role reol=roleService.findObjectById(roles);
				if(reol!=null){
					role.setRights(reol.getRights());
				}else{
					role.setRights("");
				}
			}
			role.setAddQx("0");//初始新增权限为否
			role.setChaQx("0");
			role.setDelQx("0");
			role.setEditQx("0");
			role.setParentId(roleOut.getParentId());
			role.setRoleId(this.get32UUID());
			role.setRoleName(roleOut.getRoleName());
			role.setGroupId(roleOut.getGroupId()); 
			roleService.add(role); 
			RoleOutDTO.setErrorCode(Rescode.SUCCESS, "保存菜单成功");
			return RoleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			RoleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	
	/**
	 * 删除角色信息
	 * 
	 */
	@RequestMapping(value="/deleteRole")
	@ResponseBody
	public RoleOutDTO  deleteRole(RoleInDTO roleOut){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		try {  
			Role role=new Role();
			role.setRoleId(roleOut.getRoleId());
			//是否存在下级角色数据
			List<Role> roleList=roleService.listAllRolesByPId(role);
			if(roleList.size()>0){
				roleOutDTO.setErrorCode("-2", "下级有数据时，删除失败");
			}else{
			//如果角色下有系统操作员，也不能删除	
				List<Userbase> userList=userService.listAllUserByRoldId(roleOut.getRoleId());
				if(userList.size()>0){
					roleOutDTO.setErrorCode("-3", "此角色已被使用,不能删除");
				}else{
					roleService.deleteRoleById(roleOut.getRoleId()); 
					roleOutDTO.setErrorCode(Rescode.SUCCESS, "删除角色信息成功");
				}
			}
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	
	/**
	 * 根据角色id查询详情
	 * 
	 */
	@RequestMapping(value="/findObjectById")
	@ResponseBody
	public RoleOutDTO  findObjectById(RoleInDTO roleOut){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		Role role=new Role();
		try {  
			role.setRoleId(roleOut.getRoleId()); 
			Role reol=roleService.getRoleById(roleOut.getRoleId());
			if(reol!=null){
				roleOutDTO.setAddQx(reol.getAddQx());
				roleOutDTO.setChaQx(reol.getChaQx());
				roleOutDTO.setDelQx(reol.getDelQx());
				roleOutDTO.setEditQx(reol.getEditQx());
				roleOutDTO.setParentId(reol.getParentId());
				roleOutDTO.setRights(reol.getRights());
				roleOutDTO.setRoleId(reol.getRoleId());
				roleOutDTO.setRoleName(reol.getRoleName());
				roleOutDTO.setErrorCode(Rescode.SUCCESS, "查询信息成功");
			}else{
				roleOutDTO.setErrorCode(Rescode.NOT_ROCOLD, "未查询到相关记录");
			}
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	
	/**
	 * 保存角色菜单权限
	 * 
	 */
	@RequestMapping(value="/saveMenuqx")
	@ResponseBody
	public RoleOutDTO  saveMenuqx(RoleInDTO roleInDTO){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		try { 
			roleService.saveMenuqx(roleInDTO.getRoleId(),roleInDTO.getMenuIds());
			roleOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
	/**
	 * 保存角色菜单权限
	 * 
	 */
	@RequestMapping(value="/saveB4Button")
	@ResponseBody
	public RoleOutDTO  saveB4Button(RoleInDTO roleInDTO){
		RoleOutDTO roleOutDTO=new RoleOutDTO();
		try { 
			roleService.saveB4Button(roleInDTO.getRoleId(),roleInDTO.getMenuIds(),roleInDTO.getMsg());
			roleOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return roleOutDTO;
		} catch (Exception e) { 
			e.printStackTrace();
			roleOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
		}
		return null;
	}
	
}
